import ddddocr


def match_gap_with_dddocr(bg_path, block_path):
    # 创建 dddocr 实例
    ocr = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)

    # 读取原始图片
    with open(bg_path, 'rb') as f:
        bg_bytes = f.read()
    with open(block_path, 'rb') as f:
        block_bytes = f.read()

    # 计算滑块位置
    res = ocr.slide_match(bg_bytes, block_bytes,simple_target=True)
    print("识别结果:::",res)


    return res

import re
import json

def demo_par_res(response_text):
    # 1. 提取大括号内容为json
    m = re.search(r'cb\((\{.*?\})\);?$', response_text)
    result_json = json.loads(m.group(1))
    print(result_json)
def parse_val_validate_response(response_text):
    try:
        # 尝试正则
        m = re.search(r'cb\s*\((\{.*\})\);?', response_text, re.DOTALL)
        if m:
            json_str = m.group(1)
        else:
            # Fallback: 手动提取
            start = response_text.find('cb(')
            end = response_text.rfind(');')
            if start == -1 or end == -1:
                print("没有匹配到 cb() 的包裹内容")
                return None
            json_str = response_text[start+3:end]
        outer = json.loads(json_str)
        result_str = outer.get("Data", {}).get("Result")
        if not result_str:
            print("Data.Result为空")
            return None
        result = json.loads(result_str)
        return result
    except Exception as e:
        print(f"解析validate结果异常: {e}")
        return None

# ==== 测试 ====
if __name__ == "__main__":
    # match_gap_with_dddocr('bg.jpg', 'slice.png')

    response = """
    /**/ typeof cb === 'function' && cb({"ReturnCode":"0","Msg":"成功","Data":{"Result":"{\\"message\\":null,\\"score\\":\\"88\\",\\"success\\":true,\\"validate\\":\\"372377d6077e6359a339342ff1e60f31\\",\\"captcha_type\\":null}"}})
    """
    parsed = demo_par_res(response)
    # 输出: {'message': None, 'score': '88', 'success': True, 'validate': '372377d6077e6359a339342ff1e60f31', 'captcha_type': None}
